<?php

// ideally one day this can do more than one image... 
// they would be stacked up to crop all at once in 
// Impromptu.. thus returning an array
date_default_timezone_set('UTC');
/*ini_set('display_errors',1);
ini_set('log_errors',1);
error_reporting(E_ALL);*/

class Upload extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->load->helper(array('url'));
		$this->load->library('GdImage', '', 'gdimg');
	}
	
	public function action(){
		
		include ('fineuploader.php');
		$allowedExtensions = array('jpeg','jpg','gif','png');
		$sizeLimit = 2 * 1024 * 1024; // max file size in bytes
		$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
		
		$result = $uploader->handleUpload(UPLOAD_DIR, false, md5(uniqid())); //handleUpload($uploadDirectory, $replaceOldFile=FALSE, $filename='')
		
		$gd = $this->gdimg;
		// step 1: make a copy of the original
		$filePath = UPLOAD_DIR . $result['filename'];
		$copyName = $gd->createName($result['filename'], '_FULLSIZE');
		$gd->copy($filePath, UPLOAD_DIR.$copyName);
		
		// step 2: Scale down or up this image so it fits in the browser nicely, lets say 500px is safe
		$oldSize = $gd->getProperties($filePath);
		$newSize = $gd->getAspectRatio($oldSize['w'], $oldSize['h'], 380, 0);
		$gd->resize($filePath, $newSize['w'], $newSize['h']);
		
		// step 3: handled in crop.php!
		
		// to pass data through iframe you will need to encode all html tags
		echo json_encode($result);exit();
	}
}








